﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;
using NHibernate.Criterion;



namespace Com.CMS263.DAO
{
   public class ArticleDAO
    {
       public bool Add(Article art)
       {
           DBHelper.TransInsert(art);
           return true;
       }
       public bool Modify(Article art)
       {
           DBHelper.TransSaveOrUpdate(art);
           return true;
       }
       public bool TrueDel(int id)
       {
           DBHelper.ExecuteHQL(string.Format("delete from Article where id={0}",id));
           return true;
       }
       public bool Modify(int id)
       {
           DBHelper.ExecuteHQL(string.Format("update  Article set isShow=0 where id={0}", id));
           return true;
       }

       public Article GetArticleById(int id)
       {

           return (Article)DBHelper.GetObject(typeof(Article), id);
       }
       public IList<Article> GetArticleByPid(int pid,int page,int pageSize)
       {
           IQuery iq = DBHelper.ExecuteHQL("from Article where typeID="+pid.ToString()).SetFirstResult((page-1)*pageSize).SetMaxResults(pageSize);
           if (iq.List<Article>().Count>0)
           {
               return iq.List<Article>();
           }
           else 
           {
               return null;
           }
       }




       public Article GetArticleByPid(int pid)
       {
           IQuery iq = DBHelper.ExecuteHQL("from Article where typeID=" + pid.ToString());
           if (iq.List<Article>().Count>0)
           {
               return iq.List<Article>().First();
           }
           else
           {
               return null;
           }
       }

       public int GetTotalByPid(int pid)
       {
           return  Convert.ToInt32( DBHelper.ExecuteHQL("select count(id) from Article where typeID="+pid).UniqueResult());
       }

       public IList<Article> GetArticleById(int pid, int page, int pageSize, string key)
       {

          // IQuery iq = DBHelper.ExecuteHQL("from Article where  typeID=" + pid.ToString()).SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize);
           ISession session = DBHelper.GetISession;
           IList<Article>  list=session.CreateCriteria(typeof(Article)).Add(Expression.Like("title","%"+key+"%")).Add(Expression.Eq("typeID",pid)).SetFirstResult((page-1)*pageSize).SetMaxResults(pageSize).List<Article>();
           if (list.Count > 0)
           {
               return list;
           }
           else
           {
               return null;
           }
       }

       public int GetTotalByPid(int pid, string key)
       {
           ISession session = DBHelper.GetISession;
           return session.CreateCriteria(typeof(Article)).Add(Expression.Like("title","%"+key+"%")).Add(Expression.Eq("typeID",pid)).List().Count;
       }

       public IList<Article> GetTopArticleByPid(int pid, int top)
       {
           return DBHelper.ExecuteHQL("from Article where typeID=" + pid.ToString() + " order by addDate desc ").SetFirstResult(0).SetMaxResults(top).List<Article>();
       }

       public void DeleteArtById(int id)
       {
           DBHelper.TransDeleteByID("Article",id);
       }
    }
}
